package com.epalmpay.controller.ys.shop;

import javax.annotation.Resource;

import com.epalmpay.entity.Userbase;
import com.epalmpay.entity.YsShopCar;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.service.management.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.system.ShopOutDTO;
import com.epalmpay.dto.yunliu.shop.YsShopInDTO;
import com.epalmpay.dto.yunliu.shop.YsShopListOutDTO;
import com.epalmpay.dto.yunliu.shop.YsShopOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.Shop;
import com.epalmpay.service.ys.shop.IYsShopService;
import com.epalmpay.util.Tools;

import java.util.Date;

@Controller
@RequestMapping(value="/merchant/shop/ys")
public class YsShopController extends BaseController{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource  
    private IYsShopService ysShopService ;
	@Resource
	private IUserService userService;
		/**
		 * 显示门店分页列表
		 * 
		 */
		@RequestMapping(value="/list")
		@ResponseBody
		public YsShopListOutDTO listUser(YsShopInDTO shopInDTO, PageDTO pageDTO){
		YsShopListOutDTO shopList=null;
		Page page=Tools.pageEmtity(pageDTO);
			 try {
				shopList=ysShopService.shopList(shopInDTO, page);
				shopList.setPage(page);
				shopList.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopList;
			} catch (ApplicationException ae) {
				 shopList.setErrorCode(ae.errCode, ae.errMsg);
				 ae.printStackTrace();
			 }
			 catch (Exception e) {
				 shopList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				 e.printStackTrace();
			 }
			return shopList;
		}
		
		/**
		 * 修改状态
		 * @param
		 * @return
		 */
		@RequestMapping(value="/updateStatus")
		@ResponseBody
		public YsShopOutDTO  updateStatus(YsShopInDTO shopInDTO){
			YsShopOutDTO ysShopOutDTO=new YsShopOutDTO();
			try {
				if(shopInDTO.getStatus()==1){//启用的时候去看有没设置
					YsShopCar ysShopCar = ysShopService.queryIdByShopId(shopInDTO);
					if(ysShopCar ==null){//说明没有设置预采购交易分润
						ysShopOutDTO.setErrorCode("-1", "请先设置分润交易比例");
						return ysShopOutDTO;
					}
				}
					Shop shop1 = ysShopService.queryUserIdByShopId(shopInDTO.getId());
					Userbase userbase = new Userbase();
					userbase.setId(shop1.getUserId());
					userbase.setGmtModified(new Date());
					if(shopInDTO.getStatus()==1){
						userbase.setStatus(BizEnum.UserStatus.Normal.getType());//启用
					}else{
						userbase.setStatus(BizEnum.UserStatus.Stop.getType());//禁用无法登入了
					}
					userService.updateStatus(userbase);


				Shop  shop = new Shop();
				shop.setId(shopInDTO.getId());
				shop.setStatus(shopInDTO.getStatus());
				shop.setGmtModified(new Date());
				ysShopService.updateStatus(shop);

				
				ysShopOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
				return ysShopOutDTO;
			} catch (Exception e) { 
				ysShopOutDTO.setErrorCode("-1", "处理出现异常");
				e.printStackTrace();
			}
			return null;
		}


	/**
	 * 去修改交易预采购分润设置
	 *
	 */
	@RequestMapping(value="/goEditUpdateShopRate")
	@ResponseBody
	public YsShopOutDTO goEditUpdateShopRate(YsShopInDTO shopInDTO)throws Exception{
		YsShopOutDTO ysShopOutDTO=new YsShopOutDTO();
		try {

			ysShopOutDTO=ysShopService.queryShopRateByshopId(shopInDTO);

			ysShopOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			return ysShopOutDTO;
		} catch (Exception e) {
			ysShopOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return ysShopOutDTO;
	}



		/**
		 * 创建门店
		 * 
		 */
		@RequestMapping(value="/saveShop")
		@ResponseBody
		public YsShopOutDTO SaveShop(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=new YsShopOutDTO();
			Boolean flag;
			 try {
				 flag=ysShopService.saveshop(shopInDTO);
				 if(flag){
					 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
					 
				 }else{
					 shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				 }
				return shopOutDTO;
			} catch (ApplicationException ae) {
				 shopOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				 ae.printStackTrace();
			 }catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopOutDTO;
		}
		

		/**
		 * 查询门店详情 
		 * 
		 */
		@RequestMapping(value="/selectShopInfo")
		@ResponseBody
		public YsShopOutDTO  AelectShopInfo(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=null;
			 try {
				 shopOutDTO=ysShopService.selectShopInfo(shopInDTO);
				 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopOutDTO;
			}catch (ApplicationException ae) {
				 shopOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				 ae.printStackTrace();
			 }
			catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}

			return shopOutDTO;
		}


		@RequestMapping(value="/queryMerchantName")
		@ResponseBody
		public YsShopOutDTO  queryMerchantName(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=null;
			 try {
				 shopOutDTO=ysShopService.queryMerchantName(shopInDTO);
				 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopOutDTO;
			}catch (ApplicationException ae) {
				 shopOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				 ae.printStackTrace();
			 }
			catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}

			return shopOutDTO;
		}
		@RequestMapping(value="/queryName")
		@ResponseBody
		public YsShopOutDTO  queryName(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=null;
			 try {
				 shopOutDTO=ysShopService.queryName(shopInDTO);
				 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopOutDTO;
			}catch (ApplicationException ae) {
				 shopOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				 ae.printStackTrace();
			 }
			catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}

			return shopOutDTO;
		}


	/**
	 * 查询结算信息
	 *
	 */
	@RequestMapping(value="/queryShopManage")
	@ResponseBody
	public YsShopOutDTO  queryShopManage(YsShopInDTO shopInDTO){
		YsShopOutDTO shopOutDTO=null;
		try {
			shopOutDTO=ysShopService.queryShopManage(shopInDTO);
			shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return shopOutDTO;
		}catch (Exception e) {
			shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}

		return shopOutDTO;
	}


	/**
		 * 修改门店
		 * 
		 */
		@RequestMapping(value="/editShop")
		@ResponseBody
		public ShopOutDTO editShop(YsShopInDTO shopInDTO)throws Exception{
			ShopOutDTO shopOutDTO=new ShopOutDTO();
			Boolean flag;
			 try {
				 
				 flag=ysShopService.editShop(shopInDTO);
				 if(flag){
					 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
					 
//					 String content ="修改门店信息";
//					 sysLogService.addSysLog(shopInDTO.getUserId(),shopInDTO.getUserName(),null,content,shopInDTO.getGroupId());
				 }else{
					 shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				 }
				return shopOutDTO;
			}  catch (ApplicationException e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.errMsg);
			}catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			}
			return shopOutDTO;
		}

		/**
		 * 查询门店详情 
		 * 
		 */
		@RequestMapping(value="/selectShopView")
		@ResponseBody
		public YsShopOutDTO  selectShopView(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=null;
			 try {
				 shopOutDTO=ysShopService.selectShopView(shopInDTO);
				 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopOutDTO;
			} catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopOutDTO;
		}
		
		
		/**
		 * 查询服务类型
		 *
		 */
		@RequestMapping(value="/queryShopServiceType")
		@ResponseBody
		public YsShopOutDTO  queryShopServiceType(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=null;
			 try {
				 shopOutDTO=ysShopService.queryShopServiceType(shopInDTO);
				 if(shopOutDTO==null){
					 return shopOutDTO;
				 }
				 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return shopOutDTO;
			} catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopOutDTO;
		}

		
		@RequestMapping(value="/saveShopType")
		@ResponseBody
		public YsShopListOutDTO  saveShopType(YsShopInDTO shopInDTO){
			
			YsShopListOutDTO shopListOutDTO = new YsShopListOutDTO();
			try { 
	            //保存服务类型配置
				ysShopService.saveShopType(shopInDTO);
				shopListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				
				return shopListOutDTO;
			} catch (ApplicationException ae) { 
				shopListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				ae.printStackTrace();
			}
			 catch (Exception e) { 
				 shopListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopListOutDTO;
		}
		

		
		@RequestMapping(value="/goShopService")
		@ResponseBody
		public YsShopOutDTO  goShopService(YsShopInDTO shopInDTO){
			YsShopOutDTO shopOutDTO=null;
			 try {
				 shopOutDTO=ysShopService.goShopService(shopInDTO);
				 shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopOutDTO;
			} catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopOutDTO;
		}


		@RequestMapping(value="/updateShopServiceData")
		@ResponseBody
		public YsShopListOutDTO  updateShopServiceData(YsShopInDTO shopInDTO) throws ApplicationException{

			YsShopListOutDTO shopListOutDTO = new YsShopListOutDTO();
			try {
	            //修改服务
				ysShopService.updateShopServiceData(shopInDTO);
				shopListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

				return shopListOutDTO;
			} catch (Exception e) {
				 shopListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopListOutDTO;
		}

		//作废
	/*	@RequestMapping(value="/editUpdateShopRate")
		@ResponseBody
		public YsShopOutDTO  editUpdateShopRate(YsShopInDTO shopInDTO) throws ApplicationException{

			YsShopOutDTO shopOutDTO = new YsShopOutDTO();
			try {
	            //修改服务
				ysShopService.editUpdateShopRate(shopInDTO);
				shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

				return shopOutDTO;
			} catch (Exception e) {
				shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return shopOutDTO;
		}
*/

	@RequestMapping(value="/updateShopRate")
	@ResponseBody
	public YsShopOutDTO  updateShopRate(YsShopInDTO shopInDTO) throws ApplicationException{
		YsShopOutDTO ysShopOutDTO=new YsShopOutDTO();

		try {


			YsShopCar ysShopCar = ysShopService.queryIdByShopId(shopInDTO);
			if(ysShopCar !=null){
				ysShopCar.setId(ysShopCar.getId());
				ysShopCar.setNewProfitRate(shopInDTO.getThirdFeeRate());
				ysShopCar.setProfitUpdateTime(new Date());
				ysShopCar.setNewPreprofitRate(shopInDTO.getThirdPurchaseRate());//预采购分润
				ysShopCar.setPreprofitUpdateTime(new Date());
				ysShopCar.setGmtModified(new Date());
				ysShopService.updateRate(ysShopCar);
			}else{
				YsShopCar ysShopCar1 = new YsShopCar();
				ysShopCar1.setShopId(shopInDTO.getId());
				ysShopCar1.setProfitRate(shopInDTO.getThirdFeeRate());
				ysShopCar1.setProfitUpdateTime(new Date());
				ysShopCar1.setPreprofitRate(shopInDTO.getThirdPurchaseRate());//预采购分润
				ysShopCar1.setPreprofitUpdateTime(new Date());
				ysShopCar1.setGmtCreate(new Date());
				ysShopCar1.setGmtModified(new Date());

				ysShopService.insert(ysShopCar1);
			}



			ysShopOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			return ysShopOutDTO;
		} catch (Exception e) {
			ysShopOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return ysShopOutDTO;
	}




}
